/** 
 * @Title: SetStopSalesModel.java 
 * @Package com.zizo.autohome.carinfo.crawl 
 * @Description: TODO
 * @author HeYan
 * @date 2017年1月18日 下午3:29:07 
 */
package com.zizo.autohome.carinfo.crawl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zizo.autohome.carinfo.dao.OutSalesModelDAO;
import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;

/**
 * @ClassName: SetStopSalesModel
 * @Description: TODO
 * @author HeYan
 * @date 2017年1月18日 下午3:29:07
 * 
 */
public class OutSalesModel implements Runnable {
	private OutSalesModelDAO dao;
	private int cid;

	/**
	 * @Title: getPageUrl
	 * @Description:
	 * @return void http://www.autohome.com.cn/16/sale.html
	 * @author Alexis
	 */
	public void getYearUrl() {
		String url = "http://www.autohome.com.cn/" + cid + "/sale.html";
		Document doc = DocUtil.getDoc(url);
		Elements eles = doc.select("div.car_detail div.models div.modelswrap div.tabwrap td.name_d a");
		List<String> mids = new ArrayList<>();
		for (Element ele : eles) {
			String outsalesMID = ele.attr("href").split("/")[1];
			mids.add(outsalesMID);
		}
		if (mids.size() > 0) {
			dao.setSatus(mids);
		}
	}

	public OutSalesModel(OutSalesModelDAO dao, int cid) {
		super();
		this.dao = dao;
		this.cid = cid;
	}

	public void run() {
		getYearUrl();
		System.out.println(cid + "  GG");
	}

	public static void main(String[] args) {
		Connection conn = ConnectionPool.getConnection("autohome_other", "server.so.car", "3308");
		OutSalesModelDAO dao = new OutSalesModelDAO(conn);
		List<Integer> cids = dao.getIDS();
		System.out.println("CID:" + cids.size());
		ExecutorService fix = Executors.newFixedThreadPool(20);
		for (int cid : cids) {
			OutSalesModel s = new OutSalesModel(dao, cid);
			fix.execute(s);
		}
		fix.shutdown();
		while (true) {
			if (fix.isTerminated()) {
				System.out.println("FINISH");
				break;
			} else {
				try {
					Thread.sleep(1000 * 10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
